package com.example.psysystemhomework.util;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import com.example.psysystemhomework.bean.StoryBean;
import com.example.psysystemhomework.bean.UserBean;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.Map;

import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class NetUtil {

    //同步传输  用于实现发送JSon等数据  请求结果
//    public static String host = "http://127.0.0.1:80/";
    /*
     * 家的ip
     */
//    public static String host = "http://192.168.31.69:80/";
    /*
     * 公司的ip
     **/
    public static String agreement = "http://";
//    public static String port = ":8081/";
    public String host = agreement + "192.168.150.115";  //公司的ip
    public static String getAllStory_url = "/story/get/all/1/100";
    public static String loginUser_url = "/user/login";
    public static String getUserAnswer = "/answer/get/my/answer/";
    public static String uploadStory = "/story/insert";
    public static String deleteStory = "/story/delete";
    public static String getStory_StoryId = "/story/get/my/";
    public static String getAllBackImage = "/back/get/all/";
    //    获取用户心理行为分析的结果
    public static String getCircle = "/emotion/circulate";

    private Context context;

    public NetUtil(Context context) {
        this.context = context;
        ShareUtil shareUtil = new ShareUtil(context);
        host = agreement + shareUtil.getHost();
        Log.i("连接的host", "NetUtil: " + host);

    }


    public JSONObject getCircleData() {
        String url = host + getCircle;
        JSONObject jsonObject = null;
        NetUtil netUtil = new NetUtil(context);
        Gson gson = new Gson();
        ShareUtil shareUtil = new ShareUtil(context);
        UserBean user = shareUtil.getUser();
        String data = gson.toJson(user);


        String data1 = getData(url, data, "POST");
        try {
            jsonObject = new JSONObject(data1);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return jsonObject;
    }


    public JSONObject getAllBack() {
        String url = host + getAllBackImage;
        JSONObject jsonObject = null;
        String data1 = getData(url, "", "GET");
        try {
            jsonObject = new JSONObject(data1);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return jsonObject;
    }


    public String getGetAllStory(int cur, int size, String data) {
        String url = host + getAllStory_url;
        String data1 = getData(url, data, "GET");

        return data1;
    }

    public String getStory_StoryId(int story_id) {
        String url = host + getStory_StoryId + story_id;
        String data1 = getData(url, "", "GET");

        return data1;
    }

    public String loginUser(String data) {
        String url = host + loginUser_url;
        String data1 = getData(url, data, "POST");
        return data1;
    }

    public JSONObject getUserAnswer(int userId) {

        String url = host + getUserAnswer + userId + "/1/100";
        String data1 = getData(url, "", "GET");
        JSONObject jsonObject = null;
        try {
            jsonObject = new JSONObject(data1);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return jsonObject;

    }

    public JSONObject uploadStory(StoryBean storyBean) {
        JSONObject jsonObject = null;
        Gson gson = new Gson();
        String s = gson.toJson(storyBean);
        String post = getData(host + uploadStory, s, "POST");
        if (!post.isEmpty()) {
            try {
                jsonObject = new JSONObject(post);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return jsonObject;
    }


    public JSONObject deleteStory(StoryBean storyBean) {
        JSONObject jsonObject = null;
        Gson gson = new Gson();
        String s = gson.toJson(storyBean);
        String post = getData(host + deleteStory, s, "POST");
        if (!post.isEmpty()) {
            try {
                jsonObject = new JSONObject(post);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return jsonObject;

    }

    public static String getData(String url, String data, String type) {
        String result = "";
        Log.i("发送的数据", "getData: " + data);
        OkHttpClient okHttpClient = new OkHttpClient();
        RequestBody requestBody = RequestBody.create(MediaType.get("application/json"), data);
        Request request = null;

        if (type.equalsIgnoreCase("POST")) {
            request = new Request.Builder().post(requestBody).url(url).build();
        } else {
            request = new Request.Builder().get().url(url).build();

        }

        try {

            Response response = okHttpClient.newCall(request).execute();

            result = response.body().string();

            Log.i("接收的数据", "getData: " + result);

            return result;

        } catch (IOException e) {

            Log.i("接收失败", "getData: " + result);

            e.printStackTrace();

        }

        return result;

    }

//异步传输  用于实现发送JSon等数据  请求结果

    public static synchronized void getData(String url, String data, Callback callback) {

        Log.i("发送的数据", "getData: " + data);

        OkHttpClient okHttpClient = new OkHttpClient();

        RequestBody requestBody = RequestBody.create(MediaType.get("application/json"), data);

        Request request = new Request.Builder().post(requestBody).url(url).build();

        okHttpClient.newCall(request).enqueue(callback);

    }

    public boolean postMessage(Map<String, Object> map, String url) {
        // start
        OkHttpClient okHttpClient = new OkHttpClient();
        boolean flag = false;
        FormBody formBody = new FormBody.Builder()
                .add("message", map.get("message").toString()) // xiaoxi
                .add("userID", map.get("userID").toString()) // 用户ID
                .add("messageID", map.get("messageID").toString()) //
                .add("heartCount", map.get("heartCount").toString()) //
                .add("divID", map.get("divID").toString()) //
                .build();
        Request request = new Request.Builder()
                .url(url)
                .post(formBody)
                .build();
        try {
            Response response = okHttpClient.newCall(request).execute();
            String respBody = new String(response.body().bytes(), "gbk");
            System.out.println(respBody);
            flag = true;
        } catch (Exception e) {
            System.out.println("发送短信失败......");
        }
        return flag;
    }


}
